package com.eazykitchen.controller;

import com.eazykitchen.entity.Paginating;
import com.eazykitchen.entity.UserInformation;
import com.eazykitchen.service.UserService;
import com.eazykitchen.util.BaseResponse;
import com.eazykitchen.util.PassToken;
import com.eazykitchen.util.TokenUtil;
import com.eazykitchen.util.UserLoginToken;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@Api
@RestController
@RequestMapping("/user")
public class UserController {

        Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    @ApiOperation(value="用户登录", notes="用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "userName", value = "用户名", required = true,
                    dataType="string"
            ),
            @ApiImplicitParam(
                    name = "userPwd", value = "密码", required = true,
                    dataType="string"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "登录失败"),
    })
    @PostMapping("/login")
    @PassToken
    public ResponseEntity<BaseResponse> login(@RequestBody UserInformation user) throws Exception{
        UserInformation flag = userService.getUserByName(user.getUserName());
        if (flag == null){
            return BaseResponse.generateBadResponseEntity(500,"登录失败，用户不存在", "");
        }else {
            if(!flag.getUserPwd().equals(user.getUserPwd())){
                return BaseResponse.generateBadResponseEntity(500,"密码错误", "");
            }else {
                //token以后的代码修改为自动生成
                String token = "adminToken";
                HashMap obj = new HashMap();
                // obj.put("token", tokenUtil.getToken(userForBase));
                token= TokenUtil.getToken(flag);
                obj.put("token", token);
                obj.put("user", flag);
                return BaseResponse.generateOKResponseEntity(obj);
            }
        }
    }

    @PostMapping("/add")
    @UserLoginToken
    public ResponseEntity<BaseResponse> addUser(@RequestBody UserInformation userInformation)throws Exception{
        try {
            //判断用户名是否重复
            if (userService.isUserName(userInformation.getUserName())){
                return BaseResponse.generateBadResponseEntity(500, "用户名不能重复", "");
            }
            //调用service层方法
            int flag = userService.addUser(userInformation);
            if (flag == 1) {
                return BaseResponse.generateOKResponseEntity("用户新增成功", "");
            } else {
                return BaseResponse.generateBadResponseEntity(500, "用户新增失败", "");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.debug("UserController-->>addUser", e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
        }
    }

    @DeleteMapping("/delete")
    @UserLoginToken
    public ResponseEntity<BaseResponse> delUser(@RequestBody UserInformation userInformation)throws Exception{
        try {
            //判断用户名是admin
            if (userInformation!=null && 1==userInformation.getUserId()) {
                return BaseResponse.generateBadResponseEntity(500, "admin用户不能删除", "");
            }
            int flag =userService.delUser(userInformation.getUserId());
            if (flag == 1){
                return BaseResponse.generateOKResponseEntity("用户删除成功", "");
            } else {
                return BaseResponse.generateBadResponseEntity(500, "用户删除失败", "");
            }
        }catch (Exception e){
            log.debug("UserController-->>delUser", e);
            return BaseResponse.generateBadResponseEntity(500, "error"+"服务器内部错误", "");
        }
    }

    @PutMapping("/update")
    @UserLoginToken
    public ResponseEntity<BaseResponse> updateUser(@RequestBody UserInformation userInformation){
        try {
            int flag = userService.updateUser(userInformation);
            if (flag == 1) {
                return BaseResponse.generateOKResponseEntity("修改成功", "");
            } else {
                return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
        }
    }

    @GetMapping
    @UserLoginToken
    public ResponseEntity<BaseResponse<Paginating<UserInformation>>> getUserList(UserInformation userInformation) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            //获取用户列表
            // List<UserVO> userList = userService.getUserList(vo);
            Paginating<UserInformation> paginating=userService.getUserList(userInformation);
            return BaseResponse.generateOKResponseEntity(paginating);
        } catch (Exception e) {
            log.debug("获取用户列表错误"+e);
        }
        return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
    }

    @GetMapping(value = "/{userId}")
    @UserLoginToken
    public ResponseEntity<BaseResponse<UserInformation>> getUserInfo(@PathVariable("userId") int userId) {
        try {
            UserInformation user=userService.getUserById(userId);
            return BaseResponse.generateOKResponseEntity(user);
        } catch (Exception e) {
            log.debug("UserController-->>ResponseEntity", e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
        }
    }
}
